<template>
  <div id="user-modify">
    <h2 style="text-align: center;">修改用户-->{{form.id}}</h2>
    <el-form ref="form" :model="form" label-width="80px">
      <el-form-item label="账号">
        <el-input v-model="form.username"></el-input>
      </el-form-item>

        <el-form-item label="密码" prop="pass">
          <el-input type="text" v-model="form.password" ></el-input>
        </el-form-item>
		
		<el-form-item label="身份">
		  <el-input v-model="form.admin"></el-input>
		</el-form-item>

        <!-- <el-form-item label="所处工地">
          <el-input v-model="form.gdId"></el-input>
        </el-form-item> -->

		<el-form-item label="所处工地">
		  <el-select v-model="form.gdId" placeholder="请选择" >
		    <el-option
		      v-for="item in gdlist"
		      :key="item.value"
		      :label="item.gdName"
		      :value="item.gdId">
		    </el-option>
		  </el-select>
		</el-form-item>

		<el-form-item label="所用设备">
		  <el-select v-model="form.sbId" placeholder="请选择" >
		    <el-option
		      v-for="item in sblist"
		      :key="item.value"
		      :label="item.sbName"
		      :value="item.sbId">
		    </el-option>
		  </el-select>
		</el-form-item>
		

		  
	
        <!-- <el-form-item label="所用设备">
          <el-input v-model="form.sbId"></el-input>
        </el-form-item> -->

      <el-form-item>
        <el-button type="primary" @click="modifyUser()">修改</el-button>
      </el-form-item>
    </el-form>
  </div>
</template>

<script>

  export default{
    data(){
      return {
		 gdlist: [],
		 sblist: [],
        form: {
          id: '',
          username: '',
          password: '',
          admin: '',
		  gdId: '',
          sbId:''
        }
      }
    },
	created(){
		var obj = this;
		this.axios({
		  method:"get",
		  url:"http://localhost:8088/list/all",
		}).then(function(response){
		  console.log(response.data)
		 obj.gdlist=response.data.gdList
		 obj.sblist=response.data.sbList
		})
	},
    mounted() {
      var obj = this;
      this.axios({
        method:"get",
        url:"http://localhost:8088/user/getUserById/"+this.$route.params.id
      }).then(function(response){
        console.log(response.data)
        obj.form = response.data;
      })
    },
    methods:{
      modifyUser() {
        var obj = this;
        this.axios({
          method:"POST",
          url:"http://localhost:8088/user/modifyUser",
          data:obj.form
        }).then(function(response){
          var result = response.data;
          if (result.code==1000) {

            // 成功消息提示
            obj.$message({
              message: '恭喜你，修改成功',
              type: 'success'
            });

            // 延时跳转
            setTimeout(function(){
              obj.$router.push("/users/user")
            },2000)


          } else{
            obj.$message.error('错了哦，修改失败');
          }
        })
      }
    }
  }

</script>

<style>
  #user-modify{
    width: 400px;
    margin: auto;
  }
</style>
